package com.shuqing.download.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.core.MybatisConfiguration;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import com.shuqing.download.config.DruidCommonConfig;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import javax.sql.DataSource;

/**
 * 报表系统数据源的连接池配置
 *
 * @author libing
 * @date 2021/12/28 9:46
 */
@Slf4j
@Configuration
@MapperScan(basePackages = {"com.shuqing.download.mapper.owner"}, sqlSessionTemplateRef = "sqlSessionTemplateOwner")
public class OwnerDataSourceDruidConfig extends DruidCommonConfig {

    @Value("${owner.datasource.instance.owner.dbUrl}")
    private String dbUrl;
    @Value("${owner.datasource.instance.owner.username}")
    private String username;
    @Value("${owner.datasource.instance.owner.password}")
    private String password;

    @Bean("dataSourceOwner")
    public DataSource dataSource() {
        DruidDataSource datasource = new DruidDataSource();
        datasource.setUrl(dbUrl);
        datasource.setUsername(username);
        datasource.setPassword(password);
        super.dataSourceSetParam(datasource);
        return datasource;
    }

    @Bean(name = "sqlSessionFactoryOwner")
    public SqlSessionFactory sqlSessionFactory() throws Exception {
        // 创建MybatisConfiguration
        MybatisConfiguration mybatisConfiguration = new MybatisConfiguration();
        // MybatisPlus内部过滤器
        MybatisPlusInterceptor mybatisPlusInterceptor = new MybatisPlusInterceptor();
        // 将自动分页插件设置DB类型
        mybatisPlusInterceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
        // mybatisPlusInterceptor.addInnerInterceptor(new ZebraMybatisPlusInterceptor());
        // 添加自定义拦截器
        mybatisConfiguration.addInterceptor(mybatisPlusInterceptor);
        // 开启缓存
        // mybatisConfiguration.setCacheEnabled(false);
        // 使用MybatisSqlSessionFactoryBean 可以使用mybatis-BaseMapper中的方法
        MybatisSqlSessionFactoryBean sqlSessionFactoryBean = new MybatisSqlSessionFactoryBean();
        // 添加数据源
        sqlSessionFactoryBean.setDataSource(dataSource());
        // 添加MybatisConfiguration
        sqlSessionFactoryBean.setConfiguration(mybatisConfiguration);
        ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        sqlSessionFactoryBean.setMapperLocations(resolver.getResources("classpath*:/mapper/owner/*.xml"));
        return sqlSessionFactoryBean.getObject();
    }

    @Bean(name = "transactionManagerOwner")
    @Primary
    public DataSourceTransactionManager transactionManager() {
        return new DataSourceTransactionManager(dataSource());
    }

    @Bean(name = "sqlSessionTemplateOwner")
    public SqlSessionTemplate sqlSessionTemplate() throws Exception {
        return new SqlSessionTemplate(sqlSessionFactory());
    }

}